#region Copyright RenGuiYou. All rights reserved.
//=====================================================
// NeatlyFrameWork
// Author:      RenGuiyou
// Feedback: 	mailto:750539605@qq.com
//=====================================================
#endregion

using System.Collections.Generic;
using System.Text;
using UnityEngine;

namespace Neatly.UI
{
    public static class UIListPool
    {
        private const int CACHE_COUNT = 100;
        private static readonly List<List<StringInfo>> m_Queue = new();

        public static List<StringInfo> Create()
        {
            if (m_Queue.Count > 0)
            {
                var cache = m_Queue[0];
                m_Queue.RemoveAt(0);
                cache.Clear();
                return cache;
            }
            return new List<StringInfo>();
        }

        public static void Release(List<StringInfo> list)
        {
            if (m_Queue.Count >= CACHE_COUNT) return;
            list.Clear();
            m_Queue.Add(list);
        }

        public static string GetString(List<StringInfo> list)
        {
            var sb = UIStringBuilder.Create();
            for (var i = 0; i < list.Count; i++)
            {
                var info = list[i];
                if (info.isNewLine)
                {
                    sb.Append(ConstChar.NEW_LINE);
                }
                else
                {
                    sb.Append(info.GetTabString());
                }
            }
            return UIStringBuilder.GetStringAndRelease(sb);
        }

        public static void Log(List<StringInfo> list)
        {
            foreach (var item in list)
            {
                Debug.Log(item.ToString());
            }
        }
    }
    
    public struct StringInfo
    {
        public string text { get; set; }
        public bool isItalic { get; set; }
        public bool isBold { get; set; }
        public Color color { get; set; }
        public bool isNewLine { get; set; }
        
        public string ToString()
        {
            return
                new StringBuilder()
                    .Append("text:").AppendLine(text)
                    .Append("isItalic:").AppendLine(isItalic.ToString())
                    .Append("isBold:").AppendLine(isBold.ToString())
                    .Append("textColor:").AppendLine(color.ToString())
                    .Append("isNewLine:").AppendLine(isNewLine.ToString())
                    .ToString();
        }

        public string GetTabString()
        {
            if (this.isItalic || this.isBold)
            {
                var sb = UIStringBuilder.Create();

                sb.Append(this.text);
                if (this.isItalic)
                {
                    sb.Insert(0, "<i>");
                    sb.Append("</i>");
                }
                if (this.isBold)
                {
                    sb.Insert(0, "<b>");
                    sb.Append("</b>");
                }
                return UIStringBuilder.GetStringAndRelease(sb);
            }

            return this.text;
        }
        
    }
}